package com.woniu.woniuk16.controller;

import com.woniu.woniuk16.commens.ComUtil;
import com.woniu.woniuk16.dto.WoniuUserDetails;
import com.woniu.woniuk16.exception.WoniuException;
import com.woniu.woniuk16.model.Result;
import com.woniu.woniuk16.service.WoniuAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("safe")
public class SafeController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;




    @GetMapping("/mes")
    public Result getMessage() throws WoniuException {
        WoniuUserDetails woniuUserDetails = (WoniuUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String s = ComUtil.emailCode(6);
        stringRedisTemplate.opsForValue().set("woniu:message:"+woniuUserDetails.getAccountId(),s,10, TimeUnit.MINUTES);
        if (woniuUserDetails.getAccountEmail()==null){
            throw new WoniuException("请先设置邮箱！",10086);
        }
        ComUtil.sendEmail("woniu验证码","您的验证码为："+s+"\n验证码将会在10分钟后失效",woniuUserDetails.getAccountEmail());
        return Result.getInstance(null).setMessage("发送成功");
    }
}
